import os
import threading
import time

import Play_mp3
import requests

def getSoundByYouDao(value):
    url = f'http://dict.youdao.com/dictvoice?type=0&audio={value}'

    music = requests.get(url).content
    filename = f'resource/temp/{value}.mp3'
    with open(filename, 'wb') as f:
        f.write(music)



def play_sound(value):
    """
    播放某个单词的音节 用线程运行
    :param value:
    :return:
    """

    filename = f'resource/temp/{value}.mp3'
    def _inner():
        print("hellowo")
        if not os.path.exists(filename):
            getSoundByYouDao(value)

        import pygame
        pygame.mixer.init()
        pygame.mixer.music.load(filename)
        pygame.mixer.music.play()
        time.sleep(10)

    thread1 = threading.Thread(target=_inner)
    thread1.start()


if __name__ == '__main__':
    getSoundByYouDao("good")
